const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_id: "",
        order_goods_id: "",
        reason_list: [],
        index: 0
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var self = this;
        wx.getStorage({
            key: 'key',
            success: res => {
                self.setData({
                    key: res.data
                })
            }
        })
        self.setData({
            order_id: options.order_id,
            order_goods_id: options.order_goods_id
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_refund&op=refund_form',
            data: {
                key: self.data.key,
                order_id: self.data.order_id,
                order_goods_id: self.data.order_goods_id
            },
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == 200) {
                    self.setData({
                        goods: res.data.datas.goods,
                        order: res.data.datas.order,
                        reason_list: res.data.datas.reason_list,
                    })
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            }
        })
    },
    toStore: function (e) {
        let store_id = e.currentTarget.dataset['store_id'];
        wx.navigateTo({
            url: "../store/store?store_id=" + store_id
        })
    },
    chooseImage: function (e) {
        let index = e.currentTarget.dataset['index'];
        let rec_id = e.currentTarget.dataset['rec_id'];
        var self = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original'],
            success: function (res) {
                var tempFilePaths = res.tempFilePaths;
                wx.uploadFile({
                    url: app.globalData.ApiUrl + '/index.php?act=sns_album&op=file_upload', //里面填写你的上传图片服务器API接口的路径 
                    filePath: tempFilePaths[0],
                    name: 'file',
                    header: {
                        "Content-Type": "multipart/form-data"//记得设置
                    },
                    formData: {
                        key: self.data.key
                    },
                    success: function (res) {
                        if (res.statusCode = 200) {
                            var data = JSON.parse(res.data);
                            if (data.code = 200) {
                                self.imgSet(rec_id, index, data.datas.file_name, data.datas.file_url)
                            } else {
                                wx.showToast({
                                    title: '上传失败',
                                    icon: 'none',
                                    duration: 2000
                                })
                            }

                        }
                    }
                })
            }
        })
    },
    formSubmit: function (e) {
        var self = this;
        var param = e.detail.value;
        param.key = self.data.key;
        param.order_id = self.data.order_id;
        param.order_goods_id = self.data.order_goods_id;
        param.refund_type = 1;
        if (parseFloat(param.refund_amount) > parseFloat(self.data.goods.goods_pay_price) || isNaN(parseFloat(param.refund_amount)) || parseFloat(param.refund_amount) == 0) {
            wx.showToast({
                title: '退款金额不能为空，或不能超过可退金额',
                icon: 'none'
            })
            return false;
        }
        if (param.buyer_message.length == 0) {
            wx.showToast({
                title: '请填写退款说明',
                icon: 'none'
            })
            return false;
        }
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_refund&op=refund_post',
            data: param,
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            success: (res) => {
                if (res.data.code == 200) {
                    wx.redirectTo({
                        url: '../member_refund/member_refund'
                    })
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        });
    },
    changeReason: function (e) {
        this.setData({
            index: e.detail.value
        })
    }

})